// pages/orderlist/cancelafterpay/cancelafterpay.js
var reasons = ["拍错/不喜欢/价格较贵", "长时间没发货", "无法支付订单", "商品缺货", "收货人信息有误", "其他"];
var username,phone;
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var order = JSON.parse(options.order);
    // console.log(order);
    username = '';
    phone = '';
    this.setData({
      goods_reason: "请选择",
      order: order
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.shareToIndex();
  },
  backoffReason: function () {
    var that = this;
    wx.showActionSheet({
      itemList: reasons,
      success: function (res) {
        that.setData({
          goods_reason: reasons[res.tapIndex]
        });
      }
    });
  },
  submitCancel: function () {
    if (username == '' || phone == '' || this.data.goods_reason=='请选择'){
      wx.showToast({
        title: '信息不完整',
        image:'../../imgs/tips.png'
      });
      return;
    }
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定取消订单吗',
      success: function (res) {
        if (res.confirm) {
          that.didCancel();
        }
      }
    });
  },
  didCancel:function(){
    var info = {
      order_id: this.data.order.order_id,
      user_note: this.data.goods_reason,
      consignee: username,
      mobile: phone
    };
    var url = '/index.php/api/Order/record_refund_order';
    app.request(url, info).then(this.returnGoodsCallBack.bind(this));
  },
  returnGoodsCallBack: function (res) {
    wx.showToast({
      icon: 'none',
      title: res.msg,
      duration: 2000,
    });
    var delayid = setTimeout(function () {
      clearTimeout(delayid);
      wx.navigateBack({});
    }, 2000);
  },
  notesInput:function(event){
    var itype = event.currentTarget.dataset.itype;
    if(itype == '1'){
      username = event.detail.value;
    }
    else{
      phone = event.detail.value;
    }
  }
})